package com.sisi.web;

import entity.Teacher812;
import util.JavaUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet("/teacher812")
public class Teacher812Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");

        String te = req.getParameter("te");
        if (te == null || te.equals("")){
            te = "selectAll";
        }
        switch (te){
            case "selectAll" :
                selectAll(req,resp);
                break;
            case "deleteById" :
                deleteById(req,resp);
                break;
            case "insert" :
                insert(req,resp);
                break;
            case "selectById" :
                selectById(req,resp);
                break;
            case "update" :
                update(req,resp);
                break;
        }
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        ArrayList<Teacher812> arrayList = new ArrayList<>();
        try {
            connection = JavaUtil.getConnection();
            String sql = "SELECT teacherId,teacherName,age,gender FROM teacher812 ";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while (resultSet.next()){
                int id = resultSet.getInt("teacherId");
                String name = resultSet.getString("teacherName");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                Teacher812 teacher812 = new Teacher812(id,name,age,gender);
                arrayList.add(teacher812);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JavaUtil.close(connection,statement,resultSet);
        }

        req.setAttribute("T812",arrayList);
        req.getRequestDispatcher("/student813_insert.jsp").forward(req,resp);

    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int id = Integer.parseInt(req.getParameter("id"));//是不是对应name
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JavaUtil.getConnection();
            String sql = "DELETE FROM teacher812 WHERE teacherId=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,id);
            int count = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JavaUtil.close(connection,statement,null);
        }
        resp.sendRedirect(req.getContextPath() + "/teacher812?te=selectAll");

    }

    private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String name = req.getParameter("name");
        int age = Integer.parseInt(req.getParameter("age"));
        String gender = req.getParameter("gender");
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JavaUtil.getConnection();
            String sql = "INSERT INTO teacher812(teacherName,age,gender) VALUES (?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1,name);
            statement.setInt(2,age);
            statement.setString(3,gender);
            int count = statement.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JavaUtil.close(connection,statement,null);
        }
        resp.sendRedirect(req.getContextPath() + "/teacher812?te=selectAll");

    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("selectById");
        int id = Integer.parseInt(req.getParameter("id"));
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        Teacher812 teacher812 = null;
        try {
            connection = JavaUtil.getConnection();
            String sql = "SELECT teacherName,age,gender FROM teacher812 WHERE teacherId=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,id);
            System.out.println(statement);
            resultSet =statement.executeQuery();
            while (resultSet.next()) {
                String name = resultSet.getString("teacherName");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                teacher812 = new Teacher812(id, name, age, gender);
            }
            System.out.println(teacher812);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JavaUtil.close(connection,statement,resultSet);
        }
        req.setAttribute("Te812",teacher812);
        req.getRequestDispatcher("/teacher812_update.jsp").forward(req,resp);

    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("update");

        int id = Integer.parseInt(req.getParameter("id"));
        String name = req.getParameter("name");
        int age = Integer.parseInt(req.getParameter("age"));
        String gender = req.getParameter("gender");
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JavaUtil.getConnection();
            String sql = "UPDATE teacher812 SET teacherName = ?,age = ?,gender = ? WHERE teacherId = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1,name);
            statement.setInt(2,age);
            statement.setString(3,gender);
            statement.setInt(4,id);
            int count = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JavaUtil.close(connection,statement,null);
        }
        resp.sendRedirect(req.getContextPath() + "/teacher812?te=selectAll");
    }
}
